package com.sarnath.sardoop.modules.hadoop.entity;

import java.sql.Date;
import java.text.DecimalFormat;

import org.apache.hadoop.fs.FileStatus;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sarnath.sardoop.common.utils.DateUtils;

public class HdfsFileInfo {
	private static String[] sizeUnit={"B","K","M","G"};
	private String name;
	private String fullName;
	/**
	 * bytes的可读格式
	 */
	private String length;
	/**
	 *文件或目录空间
	 */
	private long bytes;
	private boolean isdir;
	private long modification_time;
	@JsonIgnore
	private boolean isfile;
	@JsonIgnore
	private short block_replication;
	@JsonIgnore
	private long blocksize;
	@JsonIgnore
	private long access_time;
	//@JsonIgnore
	private String permission;
	//@JsonIgnore
	private String owner;
	//@JsonIgnore
	private String group;
	@JsonIgnore
	private String symlink;

	public HdfsFileInfo(FileStatus status) {
		this.fullName = status.getPath().toUri().getPath();
		this.name = fullName.substring(fullName.lastIndexOf("/")+1);
		this.bytes=status.getLen();
		this.length = calcLength();
		this.isdir = status.isDirectory();
		this.isfile = status.isFile();
		this.modification_time = status.getModificationTime();
		this.owner=status.getOwner();
		this.group=status.getGroup();
		this.permission=status.getPermission().toString();
	}
	
	private String calcLength() {
		if(bytes==0){
			return "0";
		}
		DecimalFormat df1 = new DecimalFormat("####.##"); 
		float len=bytes;
		for(int i=0;i<4;i++){
			if(len<1024){
				return df1.format(len)+sizeUnit[i];
			}
			len=len/1024;
		}
		return df1.format(len)+"T";
	}
	
	public static String[] getSizeUnit() {
		return sizeUnit;
	}

	public static void setSizeUnit(String[] sizeUnit) {
		HdfsFileInfo.sizeUnit = sizeUnit;
	}


	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isIsdir() {
		return isdir;
	}

	public void setIsdir(boolean isdir) {
		this.isdir = isdir;
	}

	public boolean isIsfile() {
		return isfile;
	}

	public void setIsfile(boolean isfile) {
		this.isfile = isfile;
	}

	public String getModification_time() {
		return DateUtils.formatDateTime(new Date(modification_time));
	}

	public void setModification_time(long modification_time) {
		this.modification_time = modification_time;
	}

	public short getBlock_replication() {
		return block_replication;
	}

	public void setBlock_replication(short block_replication) {
		this.block_replication = block_replication;
	}

	public long getBlocksize() {
		return blocksize;
	}

	public void setBlocksize(long blocksize) {
		this.blocksize = blocksize;
	}

	public long getAccess_time() {
		return access_time;
	}

	public void setAccess_time(long access_time) {
		this.access_time = access_time;
	}

	public String getPermission() {
		return permission.toString();
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getSymlink() {
		return symlink;
	}

	public void setSymlink(String symlink) {
		this.symlink = symlink;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public long getBytes() {
		return bytes;
	}

	public void setBytes(long bytes) {
		this.bytes = bytes;
		this.length = calcLength();
	}

}
